package it.antonio.ilpizze.func.core;

import it.antonio.ilpizze.func.Predicate;

import java.util.Collection;


public abstract class Predicates extends Workers implements Core{
	@Override
	public final void mustImplementPredicates() {
	}
	
	public static <T> Predicate<T> isNull() {
		return new Predicate<T>() {
			@Override
			public boolean match(T elem) {
				return elem == null;
			}
		};
	}

	public static <T> Predicate<T> isNotNull() {
		return new Predicate<T>() {
			@Override
			public boolean match(T elem) {
				return elem != null;
			}
		};
	}
	public static <T> Predicate<T> isEqualTo(final T target) {
	    return new Predicate<T>() {
			@Override
			public boolean match(T elem) {
				return elem.equals(target);
			}
		};
	  }

	public static <T> Predicate<T> or(final Predicate<T> p1, final Predicate<T> p2) {
		return new Predicate<T>() {
			@Override
			public boolean match(T elem) {
				if (p1.match(elem))
					return true;
				return p2.match(elem);
			}
		};
	}

	public static <T> Predicate<T> and(final Predicate<T> p1, final Predicate<T> p2) {
		return new Predicate<T>() {
			@Override
			public boolean match(T elem) {
				if (!p1.match(elem))
					return false;
				return p2.match(elem);
			}
		};
	}

	public static <T> Predicate<T> not(final Predicate<T> p) {
		return new Predicate<T>() {
			@Override
			public boolean match(T elem) {
				return !p.match(elem);
			}
		};
	}
	public static <T> Predicate<T> in(final Collection<? extends T> coll){
		return new Predicate<T>() {

			@Override
			public boolean match(T elem) {
				return coll.contains(elem);
			}
		};
	}
	
	public static Predicate<Number> isEven(){
		return new Predicate<Number>() {
			@Override public boolean match(Number elem) {
				return elem != null ?  elem.longValue() % 2 == 0 : true; 
			}
		};
	}
	
	public static Predicate<Number> isOdd(){
		return new Predicate<Number>() {
			@Override public boolean match(Number elem) {
				return elem != null ?  elem.longValue() % 2 != 0 : false; 
			}
		};
	}
}
